package http.four4xx;

import http.MyHttpResponse;
import util.MyUtil;

import java.io.*;
import java.net.URI;
import java.net.URL;

/**
 * @author Karl
 * @Date 2023/1/4 - 16:05
 */
public class NotFound extends MyHttpResponse {
    public NotFound(){
        InputStream html = null;
        int len = 0;
        try {
             html = this.getClass().getClassLoader().getResourceAsStream("4xx.html");
            int contentLength = html.available();
            while((len = html.read())!=-1)
                addResponseBody((byte) len);
            responseHeader.put("Content-Length","Content-Length: "+contentLength+lineSeparator);
            responseLine = "HTTP/1.1 404 NOT FOUND"+lineSeparator;
            responseHeader.put("Content-Type","Content-Type: text/html; charset=utf-8"+lineSeparator);
            responseHeader.put("cache-control","cache-control: max-age=6000"+lineSeparator);
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(0);
            responseBody.clear();
            responseHeader.put("Content-Type","Content-Type: text/html; charset=utf-8"+lineSeparator);
            responseLine = "HTTP/1.1 404 NOT FOUND"+lineSeparator;
            byte[] body = ("<h1>NOT FOUND<h1>"+lineSeparator).getBytes();
            responseHeader.put("Content-Length","Content-Length: "+body.length+lineSeparator);
            addResponseBody(body,body.length);
        }finally {
            MyUtil.closeIo(html);
        }






    }
}
